import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:go_router/go_router.dart';

import '../screens/premium_screen.dart';

class PurchaseBottomSheet extends StatelessWidget {
  final String chapterTitle;
  final int chapterPrice;
  final int comicPrice;
  final String comicTitle;
  final VoidCallback? onPurchaseChapter;
  final VoidCallback? onPurchaseComic;
  final VoidCallback? onSubscribePremium;

  const PurchaseBottomSheet({
    super.key,
    required this.chapterTitle,
    required this.chapterPrice,
    required this.comicPrice,
    required this.comicTitle,
    this.onPurchaseChapter,
    this.onPurchaseComic,
    this.onSubscribePremium,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [
            Color(0xFF2D1B69),
            Color(0xFF1A0E3D),
          ],
        ),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20),
          topRight: Radius.circular(20),
        ),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // Handle bar
          Container(
            margin: const EdgeInsets.only(top: 8),
            width: 40,
            height: 4,
            decoration: BoxDecoration(
              color: Colors.white.withOpacity(0.3),
              borderRadius: BorderRadius.circular(2),
            ),
          ),

          // Header - 缩减高度
          Padding(
            padding: const EdgeInsets.all(16),
            child: Column(
              children: [
                Icon(
                  Icons.lock_outline,
                  color: Colors.amber,
                  size: 32,
                ),
                const SizedBox(height: 8),
                Text(
                  'Unlock Content',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),

          // Options - 缩减间距
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: Column(
              children: [
                // Purchase Chapter Option
                _buildCompactOption(
                  context: context,
                  icon: Icons.article_outlined,
                  title: 'Buy Chapter',
                  subtitle: chapterTitle,
                  price: '$chapterPrice Coins',
                  onTap: () {
                    Navigator.pop(context);
                    onPurchaseChapter?.call();
                  },
                  color: const Color(0xFF4CAF50),
                ),

                const SizedBox(height: 8),

                // Purchase Comic Option
                _buildCompactOption(
                  context: context,
                  icon: Icons.menu_book_outlined,
                  title: 'Buy Full Comic',
                  subtitle: comicTitle,
                  price: '$comicPrice Coins',
                  onTap: () {
                    Navigator.pop(context);
                    onPurchaseComic?.call();
                  },
                  color: const Color(0xFF2196F3),
                  isRecommended: true,
                ),

                const SizedBox(height: 8),

                // Premium Subscription Option
                GestureDetector(
                  onTap: () async {
                    Navigator.pop(context);
                    // GoRouter.of(context).pushNamed('PremiumScreen');
                    final result = await context.pushNamed('PremiumScreen');
                    if (result == true) {
                      onSubscribePremium?.call();
                    }
                  },
                  child: _buildCompactOption(
                    context: context,
                    icon: Icons.workspace_premium,
                    title: 'Go Premium',
                    subtitle: 'Unlimited Access',
                    price: 'Subscribe',
                    onTap: () async {
                      Navigator.pop(context); // 关闭底部弹窗
                    },
                    color: const Color(0xFFFF9800),
                    isPremium: true,
                  ),
                ),

                const SizedBox(height: 12),

                // Cancel Button
                GestureDetector(
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Container(
                    width: double.infinity,
                    padding: const EdgeInsets.symmetric(vertical: 12),
                    decoration: BoxDecoration(
                      color: Colors.transparent,
                      borderRadius: BorderRadius.circular(12),
                      border: Border.all(
                        color: Colors.white.withOpacity(0.3),
                        width: 1,
                      ),
                    ),
                    child: Text(
                      'Cancel',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Colors.white.withOpacity(0.8),
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),

          // Bottom padding - 缩减
          SizedBox(height: MediaQuery.of(context).padding.bottom + 16),
        ],
      ),
    );
  }

  Widget _buildCompactOption({
    required BuildContext context,
    required IconData icon,
    required String title,
    required String subtitle,
    required String price,
    required VoidCallback? onTap,
    required Color color,
    bool isRecommended = false,
    bool isPremium = false,
  }) {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: color,
        borderRadius: BorderRadius.circular(12),
        border:
            isRecommended ? Border.all(color: Colors.amber, width: 2) : null,
      ),
      child: Row(
        children: [
          // Icon
          Container(
            padding: const EdgeInsets.all(8),
            decoration: BoxDecoration(
              color: Colors.white.withOpacity(0.2),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Icon(
              icon,
              color: Colors.white,
              size: 20,
            ),
          ),

          const SizedBox(width: 12),

          // Content
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      title,
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    if (isPremium)
                      Container(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 8,
                          vertical: 2,
                        ),
                        decoration: BoxDecoration(
                          color: Colors.amber,
                          borderRadius: BorderRadius.circular(8),
                        ),
                        child: const Text(
                          'BEST',
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 10,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                  ],
                ),
                const SizedBox(height: 2),
                Text(
                  subtitle,
                  style: TextStyle(
                    color: Colors.white.withOpacity(0.9),
                    fontSize: 13,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
                const SizedBox(height: 4),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 4,
                      ),
                      decoration: BoxDecoration(
                        color: Colors.white.withOpacity(0.2),
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: Text(
                        price,
                        style: const TextStyle(
                          color: Colors.white,
                          fontSize: 12,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                    // Add Buy button
                    if (!isPremium)
                      GestureDetector(
                        onTap: () {
                          onTap?.call();
                        },
                        child: Container(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 16,
                            vertical: 6,
                          ),
                          decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(16),
                            boxShadow: [
                              BoxShadow(
                                color: Colors.black.withOpacity(0.2),
                                blurRadius: 4,
                                offset: const Offset(0, 2),
                              ),
                            ],
                          ),
                          child: Text(
                            'Buy',
                            style: TextStyle(
                              color: color,
                              fontSize: 12,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // Static method to show the bottom sheet
  static Future<void> show(
    BuildContext context, {
    required String chapterTitle,
    required int chapterPrice,
    required int comicPrice,
    required String comicTitle,
    VoidCallback? onPurchaseChapter,
    VoidCallback? onPurchaseComic,
    VoidCallback? onSubscribePremium,
  }) {
    return showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (context) => PurchaseBottomSheet(
        chapterTitle: chapterTitle,
        chapterPrice: chapterPrice,
        comicPrice: comicPrice,
        comicTitle: comicTitle,
        onPurchaseChapter: onPurchaseChapter,
        onPurchaseComic: onPurchaseComic,
        onSubscribePremium: onSubscribePremium,
      ),
    );
  }
}
